package com.apcc4m.sfpushbatch.reader;

import java.util.ArrayList;
import java.util.List;

import org.springframework.batch.item.database.AbstractPagingItemReader;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.apcc4m.sfpushbatch.repository.Notification;
import com.apcc4m.sfpushbatch.repository.NotificationRepository;
import com.apcc4m.sfpushbatch.repository.PushStatus;
import com.apcc4m.sfpushbatch.repository.PushStatusRepository;
import com.apcc4m.sfpushbatch.repository.UserRepository;

public class PushStatusPagingItemReader<T> extends AbstractPagingItemReader<PushStatus>implements InitializingBean {

    private int doReadCount = 10000;

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private NotificationRepository notificationRepository;
    @Autowired
    private PushStatusRepository pushStatusRepository;
    @Autowired
    private UserRepository userRepository;
    private Notification notification;

    /**
     * 首先执行一遍，执行完后执行doRead方法
     */
    @Override
    protected void doOpen() throws Exception {
        System.out.println("开始读取PushStatus数据");
        super.doOpen();
        super.setPageSize(doReadCount);
        notification = notificationRepository.findOneByStatusOrderByPushDateAsc(1);
        results = new ArrayList<PushStatus>();
        if (notification != null) {
            int userType = 1;
            int status = 0;

            Query query = new Query(Criteria.where("nId").is(notification.getId()));
            query.addCriteria(Criteria.where("userType").is(userType));
            query.addCriteria(Criteria.where("status").is(status));
            query.limit(doReadCount);
            // Query query = new
            // Query(Criteria.where("nId").is(notification.getId()));
            results = mongoTemplate.find(query, PushStatus.class);
            // results = pushStatusRepository.findAll();
            // results = pushStatusRepository.findByNId(notification.getId());
            // results =
            // pushStatusRepository.findByNIdAndUserTypeAndStatus(notification.getId(),
            // userType, status);
        }
    }

    /**
     * 
     * 从数据库中一条一条读取数据，doRead调用完成后执行ItemWrite类中的write方法，反复不停的调用，如果返回null，
     * 则调用doClose
     */
    @Override
    protected PushStatus doRead() throws Exception {
        return super.doRead();
        // if (notification == null) {
        // return null;
        // }
        // // 这里的数据量是根据用户数量决定的
        // // 将所有android用户查出来
        // List<PushStatus> androidPushStatusList =
        // pushStatusRepository.findByNIdAndUserTypeAndStatus(notification.getId(),
        // 1, 0);
        // if (androidPushStatusList == null || androidPushStatusList.isEmpty())
        // {
        // return null;
        // }
        // for (PushStatus item : androidPushStatusList) {
        //// if (androidPushStatusList.size() > doReadCount) {
        // PushStatus result = androidPushStatusList.get(doReadCount);
        //// doReadCount++;
        // results.add(item);
        // return result;
        //// } else {
        //// return null;
        //// }
        // }
        // return null;
        // // for (PushStatus item : androidPushStatusList) {
        // //
        // // }
        //
        // // // 将所有iOS用户查出来
        // // List<PushStatus> iOSPushStatusList =
        // // pushStatusRepository.findByNIdAndUserType(notification.getId(),
        // 2);
        // // for (PushStatus item : iOSPushStatusList) {
        // //
        // // }
    }

    /**
     * 假设从数据库中检索出50条数据,，每读取10条执行一次doReadPage results结果增加10条数据
     */
    @Override
    protected void doReadPage() {
        System.out.println("doReadPage called");
        if (notification == null) {
            return;
        }
        int userType = 1;
        int status = 0;
        Query query = new Query(Criteria.where("nId").is(notification.getId()));
        query.addCriteria(Criteria.where("userType").is(userType));
        query.addCriteria(Criteria.where("status").is(status));
        query.limit(doReadCount);
        results = mongoTemplate.find(query, PushStatus.class);
    }

    @Override
    protected void doJumpToPage(int arg0) {
        try {
            super.jumpToItem(arg0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("doJumpToPage called");
    }

    @Override
    protected void doClose() throws Exception {
        super.doClose();
        System.out.println("结束读取PushStatus数据");
    }

}
